var jsUrl="";
var nodelist;
var linklist;
var linkSymbols="arrow";
var eventChart;
var myChart;
var option;
var cgs;


require.config({
    paths:{ 
        'echarts' : jsUrl + '/static/echarts/echarts',
        'echarts/chart/bar' : jsUrl + '/static/echarts/echarts'
    }
});

function focus(param) {
	var data = param.data;
	var nodes = option.series[0].nodes;
	if (
		data.source !== undefined
		&& data.target !== undefined
	) { //点击的是边
		var sourceNode = nodes[data.source];
		var targetNode = nodes[data.target];
		console.log("选中了边 " + sourceNode.name + ' -> ' + targetNode.name + ' (' + data.weight + ')');
	} else { // 点击的是点
		console.log("选中了" + data.name + '(' + data.value + ')');
		var id = data.id;
		var recordType = data.recordType;
		if(recordType == "0"){
			window.location.href="/view/info.htm?id="+id;
		} else if (recordType == "1") {
			window.location.href="/dc/"+id+"/?op=2";
		}
	}
	console.log(param);
}

function loadmap(){
	cgs = new Array();
	nodelist = new Array();
	linklist = new Array();
	require(
            [
                'echarts',
                'echarts/chart/bar' // 使用柱状图就加载bar模块，按需加载
            ],
            function (ec) {
            	eventChart = ec;
                myChart = eventChart.init(document.getElementById('map')); 
                
                option = {
					tooltip : {
						trigger: 'item',
						formatter: '{a} : {b}'
					},
					toolbox: {
						show : true,
						feature : {
							saveAsImage : {show: true}
						}
					},
					legend: {
						x: 'left',
						data:[]
					},
					series : [
					{
						type:'force',
						categories : cgs,
						itemStyle: {
							normal: {
								label: {
									show: true,
									textStyle: {
										color: '#333'
									}
								},
								nodeStyle : {
									brushType : 'both',
									strokeColor : 'rgba(100,215,0,0.4)',
									lineWidth : 1
								}
							},
							emphasis: {
								label: {
									show: false
									// textStyle: null      // 默认使用全局文本样式，详见TEXTSTYLE
								},
								nodeStyle : {
									//r: 30
								},
								linkStyle : {}
							}
						},
						useWorker: false,
						minRadius : 15,
						maxRadius : 25,
						gravity: 1.1,
						scaling: 1.1,
						linkSymbol: linkSymbols,
						nodes:nodelist,
						links : linklist
					}
				]
			};

		var ecConfig = require('echarts/config');
		myChart.on(ecConfig.EVENT.CLICK, focus);
        myChart.setOption(option); 
		}
	);
}

loadmap();

function getBloodKinship(ctx) {
	var param = {};
	var chk =  $("input[name='metaType']:checked").val();
	var id = $("input[name='search']").val();
	param.metaType=chk;
	param.id=id;
	$.getJSON( ctx + "/dc/ancestry/ancestry.htm",param,function(data){
        if(data.success == false){
            console.log(data.msg);
        }else{
        	nodelist = data.nodesList;
    		linklist = data.linksList;
    		cgs = data.categoryList;
        	linkSymbols = "arrow";
        }
        refresh();
    });
}

function refresh(){
	
	require(
            [
                'echarts',
                'echarts/chart/bar'
            ],
            function (ec) {
            	eventChart = ec;
                myChart = eventChart.init(document.getElementById('map')); 
                
                option = {
					tooltip : {
						trigger: 'item',
						formatter: '{a} : {b}'
					},
					toolbox: {
						show : true,
						feature : {
							saveAsImage : {show: true}
						}
					},
					legend: {
						x: 'left',
						data:[]
					},
					series : [
					{
						type:'force',
						categories : cgs,
						itemStyle: {
							normal: {
								label: {
									show: true,
									textStyle: {
										color: '#333'
									}
								},
								nodeStyle : {
									brushType : 'both',
									strokeColor : 'rgba(100,215,0,0.4)',
									lineWidth : 1
								}
							},
							emphasis: {
								label: {
									show: false
									// textStyle: null      // 默认使用全局文本样式，详见TEXTSTYLE
								},
								nodeStyle : {
									//r: 30
								},
								linkStyle : {}
							}
						},
						useWorker: false,
						minRadius : 15,
						maxRadius : 25,
						gravity: 1.1,
						scaling: 1.1,
						linkSymbol: linkSymbols,
						nodes:nodelist,
						links : linklist
					}
				]
			};

		var ecConfig = require('echarts/config');
		myChart.on(ecConfig.EVENT.CLICK, focus);
        myChart.setOption(option); 
		}
	);
	
}